package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.domain.DrCustomCharge;
import com.naiterui.ehp.bs.doctor.dao.IDrCustomChargeDao;
import com.naiterui.ehp.bs.doctor.repository.IDrCustomChargeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <P>功能描述 : 医生自定义服务收费DAO实现 </P>
 * <P>File name : DrCustomChargeDaoImpl </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/3/30 上午10:51</P>
 */
@Component
public class AdDrCustomChargeDaoImpl implements IDrCustomChargeDao {

    @Autowired
    private IDrCustomChargeRepository drCustomChargeRepository;

    @Override
    public void saveOrUpdate(DrCustomCharge customCharge) {
        Long drCustomChargeOrderId = customCharge.getId();

        // 如果ID为空，那么就认为是保存，如果有ID，那么认为是更新，其实这里应该判断bean的状态
        if (drCustomChargeOrderId == null) {

            customCharge.setStatus(DrCustomCharge.BUY_STATUS_NOT);// 新创建的订单肯定是待支付的
            customCharge.setCreatedAt(new Date());// 创建时间
            customCharge.setCreatedBy("system");// 创建人
            customCharge.setVersion(0);
            drCustomChargeRepository.save(customCharge);

        } else {

            customCharge.setChangedAt(new Date());// 修改时间
            customCharge.setChangedBy("system");// 修改人
            drCustomChargeRepository.update(customCharge);

        }

    }

    /**
     * <P>功能描述 : 通过ID获取自定义服</P>
     * <P>Method Name : getCustomChargeByPatientId </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-30 21:07:12</P>
     *
     * @param doctorId       医生ID
     * @param customChargeId 自定义服务ID
     *
     * @return 自定义服务
     *
     * @since 2.7
     */
    @Override
    public DrCustomCharge getCustomChargeByDctorId(long doctorId, long customChargeId) {

        return drCustomChargeRepository.findFirstByIdAndDoctorId(customChargeId, doctorId);
    }

    /**
     * 通过主键id获取自定义服务
     * IDrCustomChargeDao.getCustomChargeById()
     *
     * @param customChargeId 自定义服务ID
     *
     * @return
     *
     * @Author fanjiandong
     * @Date 2017年4月3日
     * @since 1.0.0
     */
    @Override
    public DrCustomCharge getCustomChargeById(long customChargeId) {
        return drCustomChargeRepository.get(customChargeId);

    }

}
